﻿using System;
using Gwn.Library.MvpVm.Base;

namespace Gwn.Library.MvpVm.Extensions
{
    /// <summary>
    /// ClassBase extension
    /// </summary>
    public static class ClassBaseExtension
    {
        /// <summary>
        /// Throws the exception.
        /// </summary>
        /// <typeparam name="TException">The type of the exception.</typeparam>
        /// <param name="classBase">The class base.</param>
        /// <param name="exceptionMessage">The exception message.</param>
        public static void ThrowEx<TException>(
            this ClassBase classBase, 
            string exceptionMessage) where TException : Exception
        {
            string message = string.Format(
                "{0}: {1} Id:{2}", 
                classBase.ClassName,
                exceptionMessage,
                classBase.UniqueId);

            // Always log it
            classBase.Logger.LogException(message, classBase.GetType());

            var ex = (TException) Activator.CreateInstance(typeof(TException), message);
            throw ex;

        }
    }
}
